package fons.model.query.fun;


public class CountFun implements Function {
	private final Function function;
	
	public CountFun () {
		this.function = null;
	}
	
	public CountFun (Function fun) {
		this.function = fun;
	}

	@Override
	public Function apply(Function other) {
		if (this.function == null) {
			return new CountFun(other);
		} else {
			throw new IllegalArgumentException("can only count a single list!");
		}
	}

	@Override
	public Function apply() {
		try {
			return new Number(((ListFun) function.apply()).get().size());
		} catch (IllegalStateException e) {
			return this;
		}
	}

}
